<?php

namespace App\Http\Controllers\Api\V1\Resources;

use App\Models\BuyerNotice;
use Illuminate\Http\Resources\Json\JsonResource;
use Brick\Math\RoundingMode;

class ProductListResource extends JsonResource
{
    public function toArray($request)
    {
    	$resource = $this->resource;
    	$company = '';
    	if($resource->command_link){
    		if(strstr($resource->command_link,'jd.com')){

    			$company = "jd";
    		}elseif(strstr($resource->command_link,'163.com')){
    			$company = "wangyi";
    		}elseif(strstr($resource->command_link,'taobao') || strstr($resource->command_link,'tb.cn') || strstr($resource->command_link,'tmall')){
    			$company = "taobao";
    		}elseif(strstr($resource->command_link,'dewu')){
    			$company = "dewu";
    		}elseif(strstr($resource->command_link,'yangkeduo.com')){
    			$company = "pdd";
    			$resource->command_link = preg_replace("/&share_uin=.*?&/", "&", $resource->command_link);
    		}


    	}

        $data = [
        	'id' => $resource->id,
	        'category_id' => $resource->category_id,
	        'title' => $resource->title,
	        'title_long' => $resource->title_long,
	        'description' => $resource->description,
	        'recommendation' => [],
	        'cover_url' => $resource->cover_url,
	        'price_lowest' => $resource->price_lowest,
	        'price_market' => $resource->price_market,
	        'sold_count' => (int)$resource->sold_count,
	        'is_subsidy' => (bool)$resource->is_subsidy,
	        'is_hongkong_direct' => (bool)$resource->is_hongkong_direct,
	        'is_overseas_direct' => (bool)$resource->is_overseas_direct,
	        'is_delivery_free' => (bool)$resource->is_delivery_free,
	        'is_sf' => (bool)$resource->is_sf,
	        'is_returnable' => (bool)$resource->is_returnable,
	        'is_sale' => (bool)$resource->is_sale,
	        'is_top' => (bool)$resource->is_top,
	        'is_seckill' => (bool)$resource->is_seckill,
	        'is_own' => (bool)$resource->is_own,
	        'is_new' => (bool)$resource->is_new,
	        'is_hot' => (bool)$resource->is_hot,
	        'is_top_100' => (bool)$resource->is_top_100,
	        'is_higher_rewards' => $resource->is_higher_rewards,
	        'is_ice' => $resource->is_ice,
	        'is_quality' => $resource->is_quality,
	        'is_low' => $resource->is_low,
	        'is_group' => $resource->is_group,
	        'is_support' => $resource->is_support,
	        'command_user_id' => $resource->command_user_id,
	        'command_user_name' => isset($resource->commandUser) ? $resource->commandUser->name : '',
	        'command_link' => $resource->command_link,
	        'command_user_avatar' => isset($resource->commandUser) ? img_path($resource->commandUser->avatar) : '',
	        'command_company' => $company,
	        'command_star' => $resource->command_star,
	        'apples' =>  (string)big_decimal($resource->price_lowest)->dividedBy(cs('sale')->apple_actual_price, 2 , RoundingMode::HALF_UP),
	        'created_at' => (string)$resource->created_at,
	        'updated_at' => (string)$resource->updated_at,
	        'comment_count' => (int)$resource->comment_count,
	        'comment_favor_rate' => (int)$resource->comment_favor_rate,
	        'group_cards_number' => (int)$resource->group_cards_number,
	        'brand' => new BrandsResource($this->brand),
	        'is_group_open' => cs('sale')->is_group_open,
	        'reward_rate' => (int)$resource->reward_rate,
	        'reward_money' => (string)big_decimal($resource->price_lowest)->multipliedBy($resource->reward_rate)->dividedBy(100, 2 , RoundingMode::HALF_UP),
	        'one_person_group_rewards' => (int)cs('sale')->one_person_group_rewards * (int)$resource->reward_rate /100,
			'two_person_group_rewards' => (int)cs('sale')->two_person_group_rewards  * (int)$resource->reward_rate /100,
			'more_person_group_rewards' => (int)cs('sale')->more_person_group_rewards  * (int)$resource->reward_rate /100,
			'joins_rate' =>$resource->group_number ?  config("site.joins.{$resource->group_number}") : [],
	        'skus'=>  ProductSkuResource::collection($this->skus),
	        'category' => new CategoryResource($this->whenLoaded('category')),
	        'attribute_group' => new AttributeGroupResource($this->whenLoaded('attributeGroup')),
	        'expresses' => ProductExpressesResource::collection($this->expresses),
	        
        ];
        
        return $data;
    }
}
